package com.ibeeking.found.uaa.g.service.impl;

import com.ibeeking.found.auth.api.common.dto.LoginUserDTO;
import com.ibeeking.found.auth.api.common.query.AccountLoginQuery;
import com.ibeeking.found.auth.api.feign.GlobalLoginClient;
import com.ibeeking.found.common.config.feign.MsgFeignClient;
import com.ibeeking.found.common.enums.LoginRoleEnum;
import com.ibeeking.found.uaa.g.model.GmcUserDetails;
import com.ibeeking.found.uaa.g.service.IGmcUserDetailsService;
import com.ibeeking.nematos.exception.AuthorizationException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author ibeeking
 */
@Service
public class GmcUserDetailsServiceImpl implements IGmcUserDetailsService {

    @Resource
    private GlobalLoginClient globalLoginClient;
    @Resource
    private MsgFeignClient msgClient;

    @Override
    public GmcUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isBlank(username)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "用户名/手机号/邮箱不能为空");
        }
        AccountLoginQuery query = new AccountLoginQuery();
        query.setUserCode(username);
        return queryGLoginUser(query);
    }

    @Override
    public GmcUserDetails loadUserByPhoneAndSmsCode(String phone, String smsCode, String key) {
        if (StringUtils.isBlank(phone)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "手机号不能为空");
        }
        if (StringUtils.isBlank(smsCode)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "手机验证码不能为空");
        }
        if (StringUtils.isBlank(key)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "验证码标识不能为空");
        }
        ResponseResult<Boolean> responseResult = msgClient.checkVerificationCode(key, phone, smsCode);
        if (null == responseResult || !responseResult.getData()) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "验证码错误或已过期");
        }

        AccountLoginQuery query = new AccountLoginQuery();
        query.setMobile(phone);
        return queryGLoginUser(query);
    }

    /***
     * 封装查询登录用户
     * @param query 参数
     * @return
     */
    private GmcUserDetails queryGLoginUser(AccountLoginQuery query) {
        ResponseResult<LoginUserDTO> responseResult = globalLoginClient.queryAccountLogin(query);
        if (null == responseResult.getData()) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "用户名或密码错误");
        }
        GmcUserDetails gmcUserDetails = BeanUtil.convertBean(responseResult.getData(), GmcUserDetails.class);
        List<SimpleGrantedAuthority> roles = new ArrayList<>();
        roles.add(new SimpleGrantedAuthority(LoginRoleEnum.GLOBAL.name()));
        roles.add(new SimpleGrantedAuthority(LoginRoleEnum.GLOBAL.name()));
        gmcUserDetails.setRoles(roles);
        return gmcUserDetails;
    }

    /**
     * 封装验证码校验
     *
     * @param code
     * @param verificationCode
     */
    private void checkVerificationCode(String code, String verificationCode) {
        //比对验证码
        if (StringUtils.isBlank(code)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "验证码已过期");
        }
        if (!code.equals(verificationCode)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "验证码错误");
        }
    }
}
